<template>
  <div>
    <div class="card" style="margin-bottom: 5px;">
      <el-input v-model="data.username" style="width: 300px; margin-right: 10px" placeholder="请输入账号名称查询"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
      <el-button type="danger" @click="delBatch">批量删除</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="头像">
          <template #default="scope">
            <el-image :src="scope.row.avatar" style="width: 40px; height: 40px; border-radius: 50%"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="role">
          <template #default="scope">
            <span v-if="scope.row.role === 'USER'">普通用户</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="160">
          <template #default="scope">
            <el-button type="danger" round plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="card">
      <el-pagination background layout="->, total, prev, pager, next" v-model:page-size="data.pageSize"
                     v-model:current-page="data.pageNum" @current-change="load" :total="data.total"/>
    </div>
  </div>
</template>

<script setup>
import request from "@/utils/request";
import {reactive} from "vue";
import {ElMessageBox, ElMessage} from "element-plus";

const data = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  tableData: [],
  username: null
})

const load = () => {
  request.get('/user/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      username: data.username
    }}
  ).then(res => {
    data.tableData = res.data.list
    data.total = res.data.total
  })
}
load()

const del = (id) => {
  ElMessageBox.confirm('此操作将永久删除该用户, 确认删除?', '确认删除', {type: 'warning'}).then((res) => {
    request.delete('/user/delete/'+id).then(res => {
      if(res.code === '200') {
        ElMessage.success('操作成功')
        load()
      }else{
        ElMessage.error(res.msg)
      }
    })
 })
}

/**
 * 批量删除
 * @param rows
 */
const handleSelectionChange = (rows) => {
  data.ids = rows.map(row => row.id)
}
const delBatch = () => {
  if(!data.ids){
    ElMessage.error('请选择要删除的数据')
  }else{
    ElMessageBox.confirm('删除后无法恢复，确定要删除吗？', '确认删除', {type: "warning"}).then(res => {
      request.delete('/user/delete-batch' , {data:data.ids}).then(res => {
        if (res.code === '200') {
          load()
          ElMessage.success('操作成功')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch(err => {
    })
  }
}

const reset = () => {
  data.username = null
  load()
}
</script>